import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Pattern;

/**
 * 提取小说内容
 * 需要连着标题一起复制
 * 起点的标题 <h1></h1>
 * 内容 <main></main>
 */
public class ExtractContent {

    private static String PATH = "E:\\小说\\";

    private static String FILE_NAME = "新建文本文档.txt";

    //匹配中文字符
    private static Pattern word = Pattern.compile("[\\u4e00-\\u9fa5]+");
    //匹配全角标点符号
    private static Pattern symbol = Pattern.compile("[\\u3002|\\uff1f|\\uff01|\\uff0c|\\u3001|\\uff1b|\\uff1a|\\u201c|\\u201d|\\u2018|\\u2019|\\uff08|\\uff09|\\u300a|\\u300b|\\u3008|\\u3009|\\u3010|\\u3011|\\u300e|\\u300f|\\u300c|\\u300d|\\ufe43|\\ufe44|\\u3014|\\u3015|\\u2026|\\u2014|\\uff5e|\\ufe4f|\\uffe5]");

    public static void main(String[] args) {
        //读取FILE_NAME文件，提取内容
        try {
            File file = new File(PATH + FILE_NAME);
            BufferedReader reader = new BufferedReader(new FileReader(file));
            String line;
            while ((line = reader.readLine()) != null){
                if(line.contains("h1"))
                    System.out.println(filterHeader(line));
                else
                    System.out.println(filterContent(line));
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            System.out.println("");
        }
    }

    /**
     * 过滤标题
     * @param line
     * @return
     */
    private static String filterHeader(String line) {
        StringBuilder header = new StringBuilder();
        char[] content = new char[line.length()];
        line.getChars(0, line.length(), content, 0);
        for (char c : content) {
            if(word.matcher(String.valueOf(c)).matches()) {
                header.append(c);
            } else if(symbol.matcher(String.valueOf(c)).matches()) {
                header.append(c);
            } else if (c == ' ') {
                if(header.length() > 0)
                    header.append(c);
            }
        }
        header.append("\n");
        return header.toString();
    }

    /**
     * 过滤内容
     * @param line
     * @return
     */
    private static String filterContent(String line) {
        StringBuilder content = new StringBuilder();
        String[] p_line = line.split("<p>");
        for (String s : p_line) {
            char[] content_char = new char[s.length()];
            s.getChars(0, s.length(), content_char, 0);
            for (char c : content_char) {
                if(word.matcher(String.valueOf(c)).matches()) {
                    content.append(c);
                } else if(symbol.matcher(String.valueOf(c)).matches())
                    content.append(c);
            }
            if(content.length() > 0) {
                content.append("\n");
                content.append("\n");

            }
        }
        return content.toString();
    }
}
